package org.kl.bpm.service.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.User;
import org.kl.bf.entity.workflow.ETask;
import org.kl.bf.service.basic.UserService;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.web.base.BasicController;
import org.kl.bpm.entity.conf.BpmTaskConf;
import org.kl.bpm.repository.conf.BpmTaskConfDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

/**
 * 
 * @Author hqm
 * @CreateTime 2014年5月25日 下午8:40:29
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BpmTaskConfService {

	private BpmTaskConfDao bpmTaskConfDao;
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private UserService userService;

	@Autowired
	private RepositoryService repositoryService;
	
	protected static Map<String, ProcessDefinition> PROCESS_DEFINITION_CACHE = new HashMap<String, ProcessDefinition>();

	public BpmTaskConf getBpmTaskConf(String id) {
		return bpmTaskConfDao.findOne(id);
	}

	public void saveBpmTaskConf(BpmTaskConf entity) {
		bpmTaskConfDao.save(entity);
	}

	public void deleteBpmTaskConf(String id) {
		bpmTaskConfDao.delete(id);
	}

	public List<BpmTaskConf> getAllBpmTaskConf() {
		return bpmTaskConfDao.findAll();
	}

	public Page<BpmTaskConf> getUserBpmTaskConf(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BpmTaskConf> spec = buildSpecification(searchParams);

		return bpmTaskConfDao.findAll(spec, pageRequest);
	}

	public List<ETask> getEtaETasks(){
		TaskQuery taskQuery = taskService.createTaskQuery().taskAssignee(BasicController.getCurrentUserId()).active();
		List<Task> taskList = taskQuery.list();
		List<ETask> epiList = new ArrayList<ETask>();
		for (Task t : taskList) {
			String processDefinitionId = t.getProcessDefinitionId();
			ProcessDefinition processDefinition = getProcessDefinition(processDefinitionId);

			ETask eTask = packageTaskInfo(t, processDefinition);
			eTask.setStatus(ETask.TODO);
			epiList.add(eTask);
			if (StringUtils.isNotBlank(t.getAssignee())) {
				User user = userService.getUser(t.getAssignee());
				eTask.setAssigneeName(user == null ? "" : user.getTrueName());
			}
		}
		return epiList;
	}
	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BpmTaskConf> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<BpmTaskConf> spec = DynamicSpecifications.bySearchFilter(filters.values(), BpmTaskConf.class);
		return spec;
	}

	private ProcessDefinition getProcessDefinition(String processDefinitionId) {
		ProcessDefinition processDefinition = PROCESS_DEFINITION_CACHE.get(processDefinitionId);
		if (processDefinition == null) {
			processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
			PROCESS_DEFINITION_CACHE.put(processDefinitionId, processDefinition);
		}
		return processDefinition;
	}

	private ETask packageTaskInfo(Task task, ProcessDefinition processDefinition) {
		ETask eTask = new ETask();
		eTask.setId(task.getId());
		eTask.setName(task.getName());
		eTask.setCreateTime(task.getCreateTime());
		eTask.setPdname(processDefinition.getName());
		eTask.setPdversion(processDefinition.getVersion());
		eTask.setPid(task.getProcessInstanceId());
		return eTask;
	}
	
	@Autowired
	public void setBpmTaskConfDao(BpmTaskConfDao bpmTaskConfDao) {
		this.bpmTaskConfDao = bpmTaskConfDao;
	}

}
